package com.jtlrm.ckd.mvp.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.base.sdk.base.net.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.fragment.BaseFragment;
import com.jtlrm.ckd.entity.ThreeDataEntity;
import com.jtlrm.ckd.entity.YinShiDataEntity;
import com.jtlrm.ckd.entity.event.YinShiUpdateMessage;
import com.jtlrm.ckd.mvp.model.YinShiMode;
import com.jtlrm.ckd.mvp.view.activity.YinShidetailActivity;
import com.jtlrm.ckd.mvp.view.adapter.YinShiDataAdapter;
import com.jtlrm.ckd.util.EventBusUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 饮食数据
 */
public class YinShiDataFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "patientId";
    private String patientId;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    YinShiDataAdapter adapter;
    TextView reLiangT;
    TextView danBaiZhiT;
    TextView jiaT;
    TextView naT;
    TextView linT;
    TextView gaiT;
    TextView shuiFenT;
    YinShiMode yinShiMode;
    ArrayList<YinShiDataEntity> datas = new ArrayList<>();

    public YinShiDataFragment() {

    }

    public static YinShiDataFragment newInstance(String patientId) {
        YinShiDataFragment fragment = new YinShiDataFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, patientId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            patientId = getArguments().getString(ARG_PARAM1);
        }
    }


    @Override
    protected int setContentLayout() {
        return R.layout.fragment_yin_shi_data;
    }

    @Override
    protected void initView() {
        adapter = new YinShiDataAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(adapter);
        View header = LayoutInflater.from(context).inflate(R.layout.yinshi_data_header, recyclerView, false);
        reLiangT = header.findViewById(R.id.re_liang);
        danBaiZhiT = header.findViewById(R.id.dan_bai_zhi);
        jiaT = header.findViewById(R.id.jia);
        naT = header.findViewById(R.id.na);
        linT = header.findViewById(R.id.lin);
        gaiT = header.findViewById(R.id.gai);
        shuiFenT = header.findViewById(R.id.shuifen);
        adapter.addHeaderView(header);
        yinShiMode = new YinShiMode();
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setEnableLoadmore(false);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        smartRefreshLayout.autoRefresh();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                YinShidetailActivity.goDetail(context, datas, position, patientId);
            }
        });
    }

    private void loadData() {
        yinShiMode.queryThreeYinShiData(patientId, new CommonObserver<List<ThreeDataEntity>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
            }

            @Override
            public void onResult(List<ThreeDataEntity> data) {
                if (data != null && data.size() > 0) {
                    ThreeDataEntity entity = data.get(0);
                    setText(reLiangT, formatLine(entity.getEnergy()));
                    setText(danBaiZhiT, formatLine(entity.getProtein()));
                    setText(naT, formatLine(entity.getSodium()));
                    setText(linT, formatLine(entity.getPhosphorus()));
                    setText(jiaT, formatLine(entity.getPotassium()));
                    setText(gaiT, formatLine(entity.getCalcium()));
                    setText(shuiFenT, formatLine(entity.getWater()));

                }
            }
        }, lifecycleSubject);
        yinShiMode.querySevenYinShiData(patientId, new CommonObserver<List<YinShiDataEntity>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                closeFrshorLoadmore();
            }

            @Override
            public void onResult(List<YinShiDataEntity> data) {
                adapter.replaceData(data);
                datas.clear();
                datas.addAll(data);
                closeFrshorLoadmore();
            }
        }, lifecycleSubject);
    }

    private String formatLine(String str) {
        if (isEmpty(str)) {
            return "-";
        } else {
            return str;
        }
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(YinShiUpdateMessage message) {
        if (message != null) {
            loadData();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }

}
